﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace y12fjydbs_1
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public partial class FrmYDL : Form
    {
        #region 变量定义

        /// <summary>
        /// 烟弹数量
        /// </summary>
        int cYDNum = 208; //烟弹数量

        /// <summary>
        /// 命令发送次数
        /// </summary>
        int cCmdTimes = 0;

        private string NowTimeStr;


        CmdType cCmdType = CmdType.None;

        int cSelectCount;
        int cSelectSeconds;

        List<CheckBox> clistCheckBox = new List<CheckBox>();
        List<YDStatus> clistYDStatus = new List<YDStatus>();

        List<CheckBox> cListRowSelect = new List<CheckBox>();

        bool cCheckKZQStatus = false; //查询控制器状态 

        public bool cIsFristRun = true;
        bool cIsFiveSecond = false;

        delegate void SetTextCallback(string text, Color TextColor);

        private System.Timers.Timer CmdTimer = new System.Timers.Timer();
        private System.Timers.Timer CmdTimerCX = new System.Timers.Timer();
        private System.Timers.Timer FireTimer = new System.Timers.Timer();
        MySerialData cSerialData;

        #endregion

        #region FrmYDL 构造函数
        public FrmYDL()
        {
            InitializeComponent();
            MyGlobal.frmYDL = this;
            cSerialData = MyGlobal.GetSerialData();
            CmdTimer.Elapsed += OnCmdTimedEvent;
            CmdTimerCX.Elapsed += OnCmdTimedCXEvent;
            FireTimer.Elapsed += OnFrieTimedEvent;

            for (int i = 1; i < 26; i++)
            {
                comboBox1.Items.Add(i.ToString());
            }
        }
        #endregion

        #region 加一个新的烟弹CheckBox
        private CheckBox AddNewYDCheckBox(int i, int x, int y,int row)
        {
            var cbx = new CheckBox();
            cbx.Enabled = false;
            cbx.Margin = new Padding(0);
            cbx.CheckAlign = ContentAlignment.TopCenter;
            cbx.TextAlign = ContentAlignment.BottomCenter;
            //cbx.FlatStyle = FlatStyle.Popup;
            cbx.Location = new Point(x, y);
            cbx.Text = (i + 1).ToString();
            cbx.Tag = row;
            cbx.Font = new Font("宋体", 9);
            cbx.ForeColor = Color.White;
            cbx.Width = 35;
            cbx.Height = 40;
            //cbx.BackColor = Color.DarkCyan;
            cbx.CheckedChanged += Cbx_CheckedChanged;
            groupBox3.Controls.Add(cbx);
            clistCheckBox.Add(cbx);
            return cbx;
        }
        private void Cbx_CheckedChanged(object sender, EventArgs e)
        {
            tbxSelectNumber.Text = clistCheckBox.Count(s => s.Checked == true).ToString();
            var checkBox = sender as CheckBox;
            if (checkBox.Checked)
            {
                checkBox.BackColor = Color.Green;
            }
            else
            {
                checkBox.BackColor = Color.SkyBlue;
            }
        }
        #endregion

        #region 加一个行选中CheckBox
        private CheckBox AddNewRowSelectCheckBox(int row, int x, int y)
        {
            var cbx = new CheckBox();
            cbx.Enabled = true;
            cbx.Margin = new Padding(0);
            cbx.CheckAlign = ContentAlignment.TopCenter;
            cbx.TextAlign = ContentAlignment.BottomCenter;
            //cbx.FlatStyle = FlatStyle.Popup;
            cbx.Location = new Point(x, y);
            cbx.Text = "弹夹"+row;
            cbx.Tag = row;
            cbx.Font = new Font("宋体", 9);
            cbx.ForeColor = Color.Blue;
            cbx.Width = 50;
            cbx.Height = 40;
            cbx.BackColor = Color.SkyBlue;
            cbx.CheckedChanged += RowSelectCbx_CheckedChanged;
            groupBox3.Controls.Add(cbx);
            cListRowSelect.Add(cbx);
            return cbx;
        }

        private void RowSelectCbx_CheckedChanged(object sender, EventArgs e)
        {
            var checkBox = sender as CheckBox;
            foreach (var item in clistCheckBox)
            {
                if (item.Enabled == true)
                {
                    if ((int)item.Tag == (int)checkBox.Tag)
                    {
                        item.Checked = checkBox.Checked;
                    }
                }
            }
            tbxSelectNumber.Text = clistCheckBox.Count(s => s.Checked == true).ToString();
        }
        #endregion

        #region FrmYDL_Load
        private void FrmYDL_Load(object sender, EventArgs e)
        {
            this.Text = "左-烟弹播撒器 版本:v" + MyGlobal.Version;

            comboBox1.SelectedIndex = 0;
            comboBox1.Enabled = false;

            int x = 80;
            int y = 50;
            int row = 0;

            for (int i = 0; i < cYDNum; i++)
            {
                if(i%26 ==0)
                {
                    row++;
                    AddNewRowSelectCheckBox(row, x - 70, y); 
                }

                CheckBox cbx = AddNewYDCheckBox(i, x, y,row);
                x += cbx.Width + 3 ;

                if ((i + 1) % 26 == 0)
                {

                    y += cbx.Height + 22;
                    x = 80;
                }

                clistYDStatus.Add( new YDStatus());
            }

            DateTime myDateTime = DateTime.Now;
            NowTimeStr = myDateTime.ToString("    [HH:mm]   ");

            InfoDisp.AppendText("   您好，欢迎使用本系统！\n");
            InfoDisp.AppendText("   今天是" + myDateTime.ToString(" yyyy年MM月dd日, 现在是北京时间HH时mm分ss秒。\n"));

            NowTimeStr = DateTime.Now.ToString("    [HH:mm:ss]   ");
            cSerialData.SendCmd_CX(CmdByte.YD, CmdByte.LEFT, CmdByte.KZQZT);
            RecviveMsg("左播撒器 --> 发送烟弹控制器查询指令!", Color.LightSalmon);
            cCmdType = CmdType.控制器状态;
            ReSetCmdTimer(500);

            ButtonEnable(false, false, false);


            //for (int i = 0; i < cYDNum; i++)
            //{ 
            //    if (i < 130)
            //    {
            //        clistCheckBox[i].SafeInvoke(s => s.Enabled = true);
            //        clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.SkyBlue);
            //    }
            //    else
            //    {
            //        clistCheckBox[i].SafeInvoke(s => s.Enabled = false);
            //        clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.DarkRed);
            //    }
            //}
        }

        #endregion

        #region FrmYDL_FormClosing
        private void FrmYDL_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveFile();
            cSerialData.cStop = true;
            Application.Exit();
        }
        #endregion

        #region 界面 ZYContrl_Paint
        private void ZYContrl_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font fnt = new Font("黑体", 30);
            Rectangle rect1 = new Rectangle(0, 0, 100, 100);
            LinearGradientBrush brh = new LinearGradientBrush(rect1, Color.Red, Color.YellowGreen, LinearGradientMode.Vertical);
            Brush brh1 = new SolidBrush(Color.White);
            g.DrawString("BF-208A飞机焰弹播撒控制系统", fnt, brh1, 469, 20);
            g.DrawString("BF-208A飞机焰弹播撒控制系统", fnt, brh, 470, 22);
            //Pen pen1 = new Pen(Color.WhiteSmoke, 2);
            Pen pen1 = new Pen(Color.Bisque, 2);
            g.DrawLine(pen1, new Point(0, 105), new Point(ClientSize.Width, 105));
            Pen pen = new Pen(Color.YellowGreen, 4);
            g.DrawLine(pen, new Point(0, 105 + 2), new Point(ClientSize.Width, 105 + 2));
            Pen pen2 = new Pen(Color.Gray, 2);
            g.DrawLine(pen2, new Point(0, 105 + 5), new Point(ClientSize.Width, 105 + 5)); 

        }
        #endregion

        #region 界面时间刷新定时器

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime myDateTime = DateTime.Now;
 
            NowTimeStr = myDateTime.ToString("    [HH:mm:ss]   ");
            label3.Text = myDateTime.ToString("yyyy年MM月dd日 HH时mm分ss秒");

        }
        #endregion


        #region 控制器状态 A2 --- 命令返回
        public void RecviveMsgA2(bool isReady)
        {
            if (isReady)
            {
                CmdTimer.Enabled = false;

                cIsFiveSecond = true;
                ReSetCmdTimerCX(5000);


                TbxKZQStatus.SafeInvoke(s => s.Text = "通讯正常");
                TbxKZQStatus.SafeInvoke(s => s.BackColor = Color.Lime);

                cCheckKZQStatus = true;


                if (cIsFristRun)
                {
                    cSerialData.SendCmd_FW(CmdByte.YD, CmdByte.LEFT);
                    cCmdType = CmdType.复位;
                    cCmdTimes = 0;
                    ReSetCmdTimer(3000);
                    RecviveMsg("左播撒器 --> 发送焰弹执行控制器复位指令!", Color.LightSalmon);
                    cIsFristRun = false;
                }
                else
                {
                    ButtonEnable(true, true, true);
                }
            }
            else
            {
                cIsFiveSecond = false;
                TbxKZQStatus.SafeInvoke(s => s.Text = "未通讯");
                TbxKZQStatus.SafeInvoke(s => s.BackColor = Color.DarkRed);
                ButtonEnable(false, false, false);
            }
        }
        #endregion

        #region 复位命令 A7 ---命令返回
        public void RecviveMsgA7()
        {
            CmdTimer.Enabled = false;
            cSerialData.SendCmd_CX(CmdByte.YD,CmdByte.LEFT, CmdByte.GDCS);
            cCmdTimes = 0;
            cCmdType = CmdType.挂弹状态;
            RecviveMsg("左播撒器 --> 发送焰弹测试结果查询指令!", Color.LightSalmon);
            ReSetCmdTimer(10000);
            for (int i = 0; i < cYDNum; i++)
            {
                clistCheckBox[i].SafeInvoke(s => s.Checked = false);
                clistCheckBox[i].SafeInvoke(s => s.Enabled = false);
                clistYDStatus[i].GDStatus = false;
                clistYDStatus[i].BSStatus = false;
            }
        }
        #endregion

        #region 挂弹状态 A1--- 命令返回
        public void RecviveMsgA1(List<bool> list)
        {
            cCmdType = CmdType.None;

            for (int i = 0; i < cYDNum; i++)
            {
                clistYDStatus[i].GDStatus = list[i];
            }

            for (int i = 0; i < cYDNum; i++)
            {
                if (clistYDStatus[i].GDStatus)
                {
                    clistCheckBox[i].SafeInvoke(s=>s.Enabled = true);
                    clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.SkyBlue);
                }
                else
                {
                    clistCheckBox[i].SafeInvoke(s => s.Enabled = false); 
                    clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.DarkRed);
                }
            }

            //SetRed();

                ButtonEnable(true, true, true);
        }
        #endregion

        public void SetRed()
        { 
            for (int i = 0; i < cYDNum; i++)
            {
                if( i == 0 || i == 5 || i == 13 || i == 18 ||
                    i == 26 || i == 31 || i == 39 || i == 44 ||
                    i == 52 || i == 57 || i == 65 || i == 70 ||
                    i == 78 || i == 83 || i == 91 || i == 96 ||
                    i == 104 || i == 109 || i == 117 || i == 122 ||
                    i == 130 || i == 135 || i == 143 || i == 148 ||
                    i == 156 || i == 161 || i == 169 || i == 174 ||
                    i == 182 || i == 187 || i == 195 || i == 200 ) 
                clistCheckBox[i].SafeInvoke(s => s.Checked = false);
                clistCheckBox[i].SafeInvoke(s => s.Enabled = false);
                clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.DarkRed);
            }
        }
        #region 播撒返回 A8---命令返回
        public void RecviveMsgA8(byte bsStatus, List<bool> list)
        {
            CmdTimer.Enabled = false;
            switch (bsStatus)
            {
                //已响应命令
                case 0xFF:  
                    int t = (cSelectCount -1) * cSelectSeconds * 1000;                    
 
                    ReSetCmdTimer(t + 2000);
                    cCmdType = CmdType.查询播撒结果;

                    foreach (var item in clistCheckBox)
                    {
                        if(item.Checked)
                        {
                            item.SafeInvoke(s=>s.BackColor = Color.Yellow);
                            item.SafeInvoke(s => s.Refresh());
                        }
                    }
                    cCmdTimes = 0;
                    ReSetFireTimer(cSelectSeconds * 1000);

                    if(cSelectCount > 1)
                        FireOne();
                    return; 

                //播撒已完成
                case 0x0F:
                    cCmdType = CmdType.None;
                    FireTimer.Enabled = false;
                    break;
                //错误
                case 0x07:
                    cCmdType = CmdType.None;
                    FireTimer.Enabled = false;
                    break;
            } 

            for (int i = 0; i < cYDNum; i++)
            {
                clistYDStatus[i].BSStatus = list[i];
            }

            for (int i = 0; i < cYDNum; i++)
            {
                if(clistYDStatus[i].GDStatus)
                {
                    if(clistYDStatus[i].BSStatus)
                    {
                        clistCheckBox[i].SafeInvoke(s => s.Checked = false);
                        clistCheckBox[i].SafeInvoke(s=>s.Enabled = false);
                        clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.DarkRed);
                    }
                    else
                    {
                        clistCheckBox[i].SafeInvoke(s => s.Enabled = true);
                        if(clistCheckBox[i].Checked)
                            clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.Green);
                        else
                            clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.SkyBlue);
                    }
                }
                else
                {
                    clistCheckBox[i].SafeInvoke(s => s.Checked = false);
                    clistCheckBox[i].SafeInvoke(s => s.Enabled = false);
                    clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.DarkRed);
                }
            }
            //SetRed();
            ButtonEnable(true, true, true);
        }
        #endregion

         
        #region 终止播撒 A5---命令返回
        public void RecviveMsgA5()
        {
            cSerialData.SendCmd_CX(CmdByte.YD,CmdByte.LEFT, CmdByte.BSJG);
            RecviveMsg("左播撒器 --> 发送查询焰弹播撒结果指令!",Color.LightSalmon);

            cCmdType = CmdType.查询播撒结果;
            cCmdTimes = 0;
            ReSetCmdTimer(500);
        }
        #endregion


        #region RecviveMsg
        public void RecviveMsg(string text, Color TextColor)
        {
            if (cCmdType == CmdType.None && cbxShowFiveMsg.Checked == false && TextColor == Color.Navy)
                return;

            try
            {

                if (InfoDisp.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(RecviveMsg);
                    InfoDisp.Invoke(d, new object[] { text, TextColor });
                }
                else
                {
                    text = NowTimeStr + text + " \n";
                    InfoDisp.Focus();
                    //设置光标的位置到文本尾    
                    InfoDisp.Select(InfoDisp.TextLength, 0);
                    //滚动到控件光标处    
                    InfoDisp.SelectionFont = new Font("微软雅黑", 10.5F, FontStyle.Regular);
                    InfoDisp.SelectionColor = TextColor;
                    InfoDisp.AppendText(text);
                    InfoDisp.ScrollToCaret();
                    InfoDisp.Update();
                }
            }
            catch (Exception ee)
            {

                Msg.WriteLine(ee.Message,50,'-');
            }
        }
        #endregion


        #region 检查 --- 按钮
        private void BtnCheckL_Click(object sender, EventArgs e)
        { 
            if(cCheckKZQStatus == false)
            {
                MessageBox.Show( "通讯状态不正常!", "提示");
                return;
            }
            cSerialData.SendCmd_FW(CmdByte.YD, CmdByte.LEFT);
            RecviveMsg("左播撒器 --> 发送控制器复位指令!",Color.LightSalmon); 
            cCmdType = CmdType.复位;
            cCmdTimes = 0;
            ReSetCmdTimer(3000);

            for (int i = 0; i < cYDNum; i++)
            { 
                clistCheckBox[i].SafeInvoke(s => s.Checked = false);
                clistCheckBox[i].SafeInvoke(s => s.Enabled = false);
                clistCheckBox[i].SafeInvoke(s => s.BackColor = Color.Gray);
                clistCheckBox[i].SafeInvoke(s => s.Refresh());
                clistYDStatus[i].GDStatus = false;
                clistYDStatus[i].BSStatus = false;
            }

            ButtonEnable(false, false, false);
        }
        #endregion

        #region 播撒 --- 按钮
        private void BtnFireL_Click(object sender, EventArgs e)
        {
            byte s = 0x0A;

            if(radioButton2.Checked)
            {
                s = (byte)((comboBox1.SelectedIndex + 1) * 10);
            }
            List<bool> list = new List<bool>();
            clistCheckBox.ForEach(o => list.Add(o.Checked));


            if (list.Count(o=>o==true)<=0)
            {
                RecviveMsg(" 错误: 请选择要播撒的焰弹!", Color.Red);
                return;
            }

            if (radioButton2.Checked)
            {
                RecviveMsg("播撒方式选择: 自动播撒!", Color.LightSalmon);
                RecviveMsg($"播撒间隔时间: {comboBox1.Text}秒!", Color.LightSalmon);
            }
            else
            {
                RecviveMsg("播撒方式选择: 手动播撒!", Color.LightSalmon);
                RecviveMsg($"播撒间隔时间: {comboBox1.Text}秒!", Color.LightSalmon);
            }

            string fstr = $"共选择{list.Count(o => o == true)}个烟弹,编号 [";
            for (int i = 0; i < cYDNum; i++)
            {
                if (clistCheckBox[i].Checked)
                {
                    fstr += i + 1 + ",";
                }
            }
            fstr = fstr.TrimEnd(',');
            fstr += "]";
            RecviveMsg(fstr, Color.LightSalmon);
             
            cSelectSeconds = comboBox1.SelectedIndex + 1;
            cSelectCount = list.Count(o => o == true);
            cSerialData.SendCmd_BS(CmdByte.YD, CmdByte.LEFT, s, list);
            RecviveMsg("左播撒器 --> 发送焰弹播撒指令!",Color.LightSalmon);

            cCmdType = CmdType.播撒;
            cCmdTimes = 0;
            ReSetCmdTimer(500);

            ButtonEnable(false, false, true);
        }
        #endregion

        #region 中止播撒 --- 按钮
        private void BtnStopL_Click(object sender, EventArgs e)
        {
            cSerialData.SendCmd_TZ(CmdByte.YD,CmdByte.LEFT);
            RecviveMsg("左播撒器 --> 发送中止播撒指令!",Color.LightSalmon);
            cCmdType = CmdType.中止播撒;
            cCmdTimes = 0;
            ReSetCmdTimer(500);
            ButtonEnable(false, false, false);
        }
        #endregion

        #region 手动自动切换按钮
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            comboBox1.Enabled = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            comboBox1.Enabled = true;
        }
        #endregion

        #region 复位命令定时器
        private void ReSetCmdTimer(int ms)
        {
            CmdTimer.Interval = ms;
            CmdTimer.AutoReset = false;
            CmdTimer.Enabled = true;
        }
        #endregion

        #region 复位命令定时器CX
        private void ReSetCmdTimerCX(int ms)
        {
            CmdTimerCX.Interval = ms;
            CmdTimerCX.AutoReset = false;
            CmdTimerCX.Enabled = true;
        }
        #endregion

        #region 复位点火定时器
        private void ReSetFireTimer(int ms)
        {
            FireTimer.Interval = ms;
            FireTimer.AutoReset = true;
            FireTimer.Enabled = true;
        }
        #endregion

        #region 命令定时器-事件
        private void OnCmdTimedEvent(Object source, ElapsedEventArgs e)
        {
            Msg.WriteLine("in OnCmdTimedEvent()");
            CmdTimer.Enabled = false;

            switch (cCmdType)
            {
                case CmdType.None:
                    break;
                case CmdType.控制器状态:
                    cCmdTimes++;
                    if (cCmdTimes > 20)
                    {
                        cCmdType = CmdType.控制器状态;
                        TbxKZQStatus.Text = "未通信";
                        TbxKZQStatus.BackColor = Color.DarkRed;
                        RecviveMsg("左播撒器 --- 初始通信测试未通过!!!", Color.Red);
                        cCmdType = CmdType.None;
                        break;
                    }  
                    cSerialData.SendCmd_CX(CmdByte.YD, CmdByte.LEFT, CmdByte.KZQZT);                    
                    RecviveMsg($"左播撒器 --> 发送查询焰弹控制器状态指令! - {cCmdTimes}",Color.LightSalmon);
                    ReSetCmdTimer(500);                 
                    break; 
                case CmdType.复位:
                    cCmdTimes++;
                    if (cCmdTimes > 2)
                    {
                        RecviveMsg("左播撒器 --- 焰弹测试未通过!!!",Color.Red);
                        cCmdType = CmdType.None;
                        break;
                    }
                    else
                    {
                        cSerialData.SendCmd_FW(CmdByte.YD,CmdByte.LEFT);
                        RecviveMsg($"左播撒器 --> 发送焰弹执行控制器复位指令! - {cCmdTimes}", Color.LightSalmon);
                        ReSetCmdTimer(3000);
                        break;
                    }
                case CmdType.挂弹状态:
                    cCmdTimes++;
                    if (cCmdTimes > 2)
                    {
                        RecviveMsg("左播撒器 --- 焰弹测试未通过!!!",Color.Red);
                        cCmdType = CmdType.None;
                        break;
                    }
                    else
                    {
                        cSerialData.SendCmd_CX(CmdByte.YD, CmdByte.LEFT, CmdByte.GDCS);
                        RecviveMsg($"左播撒器 --- 发送挂弹测试指令! - {cCmdTimes}", Color.LightSalmon);
                        ReSetCmdTimer(10000);
                        break;
                    }
                case CmdType.播撒:
                    cCmdTimes++;
                    if (cCmdTimes > 2)
                    {
                        RecviveMsg("左播撒器 --- 焰弹播撒指令未响应!!!",Color.Red);
                        cCmdType = CmdType.None;
                        break;
                    }
                    else
                    {
                        cSerialData.SendCmd_CX(CmdByte.YD, CmdByte.LEFT, CmdByte.BSJG);
                        cCmdType = CmdType.播撒;
                        ReSetCmdTimer(500);
                        RecviveMsg($"左播撒器 --- 发送查询焰弹播撒结果指令! - {cCmdTimes}", Color.LightSalmon); 
                        break;
                    }
                case CmdType.查询播撒结果:
                    cCmdTimes++;
                    if (cCmdTimes > 2)
                    {
                        RecviveMsg("左播撒器 --- 焰弹播撒结果查询失败!", Color.Red);
                        cCmdType = CmdType.None;
                        break;
                    }
                    else
                    {
                        cSerialData.SendCmd_CX(CmdByte.YD, CmdByte.LEFT,CmdByte.BSJG);
                        RecviveMsg($"左播撒器 --- 发送查询焰弹播撒结果指令! - {cCmdTimes}", Color.LightSalmon);//A1
                        cCmdType = CmdType.查询播撒结果;
                        ReSetCmdTimer(500);
                        break;
                    }
                case CmdType.中止播撒:
                    cCmdTimes++;
                    if(cCmdTimes>2)
                    {
                        RecviveMsg("左播撒器 --- 终止播撒指令未响应!", Color.Red);
                        cCmdType = CmdType.None;
                    }
                    else
                    {
                        cSerialData.SendCmd_TZ(CmdByte.YD, CmdByte.LEFT);
                        RecviveMsg($"左播撒器 --- 发送中止播撒指令! - {cCmdTimes}", Color.LightSalmon);
                        cCmdType = CmdType.中止播撒; 
                        ReSetCmdTimer(500);
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 5s - 定时器 - 事件
        private void OnCmdTimedCXEvent(Object source, ElapsedEventArgs e)
        {
            if (cCmdType == CmdType.None)
            {
                if (cIsFiveSecond == false)
                {
                    TbxKZQStatus.SafeInvoke(s => s.Text = "未通讯");
                    TbxKZQStatus.SafeInvoke(s => s.BackColor = Color.DarkRed);
                    ButtonEnable(false, false, false);
                }

                cSerialData.SendCmd_CX(CmdByte.YD,CmdByte.LEFT, CmdByte.KZQZT);

                if (cbxShowFiveMsg.Checked)
                {
                    RecviveMsg("左播撒器 --> 发送查询焰弹控制器状态指令!", Color.LightSalmon);
                    Msg.WriteLine("                                                                                         左播撒器 --- 5s定时器 cCmdType == CmdType.None 左播撒器 ---!");
                }

                cIsFiveSecond = false;
            }
            else
            {
                Msg.WriteLine($"                                                                                         左播撒器 --- 5s定时器 cCmdType = {cCmdType.ToString()}");
            }
            ReSetCmdTimerCX(5000);
        }
        #region 开启关闭-5秒-定时器 
        public void StopFiveClock()
        {
            Msg.WriteLine("关闭焰弹左通道 5秒 定时器!");
            CmdTimerCX.Enabled = false;
        }
        public void StartFiveClock()
        {
            Msg.WriteLine("开启焰弹左通道 5秒 定时器!");
            ReSetCmdTimerCX(5000);
        }
        #endregion

        #endregion

        #region 点火 - 定时器 - 事件
        private void OnFrieTimedEvent(Object source, ElapsedEventArgs e)
        {
            FireOne();
        }
        private void FireOne()
        {
            int remain = clistCheckBox.Count(s => s.BackColor == Color.Yellow);
            Msg.WriteLine($"点火 - 定时器 - 剩余数量:={remain}", 100);
            if (remain <= 0)
                FireTimer.Enabled = false;
            else
            {
                foreach (var item in clistCheckBox)
                {
                    if (item.BackColor == Color.Yellow)
                    {
                        item.SafeInvoke(s => s.BackColor = Color.DarkRed);
                        Msg.WriteLine($"点火 - 定时器 - 点火编号:={item.Text}", 100);
                        break;
                    }
                }
            }
        }
        #endregion


        #region 保存日志按钮
        private void button1_Click(object sender, EventArgs e)
        {
            SaveFile();
        }
        private void SaveFile()
        {
            DateTime myDateTime = DateTime.Now;
            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + "\\LEFT-TD-log\\";

            if (!Directory.Exists(path)) // 如果该文件夹不存在则创建
                Directory.CreateDirectory(path);
            string FileName = path + myDateTime.ToString("yyyy年MM月dd日HH时mm分") + ".log";
            //   MessageBox.Show(FileName);
            using (StreamWriter sw = new StreamWriter(FileName))
            {
                sw.WriteLine(InfoDisp.Text);
            }
        }
        #endregion

        #region 按钮状态控制
        public void ButtonEnable(bool check,bool bs,bool stop)
        {
            BtnCheckL.SafeInvoke(s => s.Enabled = check);
            BtnFireL.SafeInvoke(s => s.Enabled = bs);
            BtnStopL.SafeInvoke(s => s.Enabled = stop);
        }
        #endregion


        #region 三个主界面 - 切换按钮
        private void BtnYT_Click(object sender, EventArgs e)
        {                
            StopFiveClock();
            if (MyGlobal.frmYT == null)
            {
                MyGlobal.frmYT = new FrmYT(); 
                MyGlobal.frmYT.Show(); 
                this.Hide();
            }
            else
            {
                MyGlobal.frmYT.Show(); 
                MyGlobal.frmYT.StartFiveClock();
                this.Hide();
            }
        }
        private void BtnTDR_Click(object sender, EventArgs e)
        {
            StopFiveClock();
            if(MyGlobal.frmYDR == null)
            {
                MyGlobal.frmYDR = new FrmYDR();
                MyGlobal.frmYDR.Show();
                this.Hide();
            }
            else
            {
                MyGlobal.frmYDR.Show();
                MyGlobal.frmYDR.StartFiveClock();
                this.Hide();
            }

        }
        private void BtnTDL_Click(object sender, EventArgs e)
        {
            //自己
        }
        #endregion

    }
}
